from app.db.mongoClient import ErrMongoClient, get_mongo_client
from . import dbSynPosDining, collectionDiningList
from app.globals.publicMethod import globalTimeToString, globalCurrentTimeisOrNot, getTodayAnyDate

def queryAllDining():
    '''
    查询所有餐厅
    '''
    try:
        mg_client = get_mongo_client()
        res = mg_client[dbSynPosDining][collectionDiningList].find()
        diningList = []
        for v in res:
            users = {
                'diningNo': v.get('diningNo'),
                'diningName': v.get('diningName'),
                'createTime': globalTimeToString(v.get('createTime')),
                'updateTime': globalTimeToString(v.get('updateTime')),
                'creatorUserName': v.get('creatorUserName'),
                'editorUserName': v.get('editorUserName'),
                'diningDesc': v.get('diningDesc'),
                'menuList': v.get('menuList')
            }
            diningList.append(users)

        return diningList
    except Exception as e:
        raise ErrMongoClient.data(str(e))


def queryDinings(RelationDiningNoList):
    '''
    查询指定的一些餐厅
    '''
    diningList = []
    if RelationDiningNoList is None or len(RelationDiningNoList) <= 0:
        return diningList

    mg_client = get_mongo_client()
    try:
        res = mg_client[dbSynPosDining][collectionDiningList].find()
        for v in res:
            if v.get('diningNo') in RelationDiningNoList:
                users = {
                    'diningNo': v.get('diningNo'),
                    'diningName': v.get('diningName'),
                    'createTime': globalTimeToString(v.get('createTime')),
                    'updateTime': globalTimeToString(v.get('updateTime')),
                    'creatorUserName': v.get('creatorUserName'),
                    'editorUserName': v.get('editorUserName'),
                    'diningDesc': v.get('diningDesc'),
                    'menuList': v.get('menuList')
                }
                diningList.append(users)
                
        return diningList
    except Exception as e:
        raise ErrMongoClient.data(str(e))


def querySignDining(diningNo):
    '''
    查询指定的餐厅
    '''
    mg_client = get_mongo_client()
    try:
        cond = {'diningNo': diningNo}
        res = mg_client[dbSynPosDining][collectionDiningList].find_one(cond)
        if res is None:
            return None

        data = {
            'diningNo': res.get('diningNo'),
            'diningName': res.get('diningName'),
            'createTime': globalTimeToString(res.get('createTime')),
            'updateTime': globalTimeToString(res.get('updateTime')),
            'creatorUserName': res.get('creatorUserName'),
            'editorUserName': res.get('editorUserName'),
            'diningDesc': res.get('diningDesc'),
            'menuList': res.get('menuList')
        }
        return data
    except Exception as e:
        raise ErrMongoClient.data(str(e))


def queryDiningName(id):
    '''
    查询餐厅名
    '''
    mg_client = get_mongo_client()
    try:
        res = mg_client[dbSynPosDining][collectionDiningList].find()
        for v in res:
            if v.get('diningNo') == id:
               return v.get('diningName') 
    except Exception as e:
        raise ErrMongoClient.data(str(e))


def queryAllDiningNo():
    '''
    查询所有餐厅号
    '''
    try:
        mg_client = get_mongo_client()
        res = mg_client[dbSynPosDining][collectionDiningList].find()
        RelationDiningNoList = []
        for v in res:
            RelationDiningNoList.append(v.get('diningNo'))

        return RelationDiningNoList
    except Exception as e:
        raise ErrMongoClient.data(str(e))


def queryAllDiningNoById(id):
    '''
    查询当前时间属于哪个餐次
    '''
    try:
        mg_client = get_mongo_client()
        res = mg_client[dbSynPosDining][collectionDiningList].find()
        for v in res:
            if v.get('diningNo') == id:
               menuList = v.get('menuList')
               for v in menuList:
                   mealStartTime = v['mealStartTime']
                   mealEndTime = v['mealEndTime']
                   startTimeIsoDate = getTodayAnyDate(int(mealStartTime.split(":")[0]), int(mealStartTime.split(":")[1]), int(mealStartTime.split(":")[2]))
                   endTimeIsoDate = getTodayAnyDate(int(mealEndTime.split(":")[0]), int(mealEndTime.split(":")[1]), int(mealEndTime.split(":")[2]))
                   if globalCurrentTimeisOrNot(startTimeIsoDate, endTimeIsoDate) is True:            
                        return startTimeIsoDate, endTimeIsoDate
                    
               return "", ""    
           
        return "", ""   
    except Exception as e:
        raise ErrMongoClient.data(str(e))


def queryDiningInfoByNo(diningNo):
    '''
    根据餐厅号查询餐厅名及其下档口信息
    '''
    try:
        mg_client = get_mongo_client()
        res = mg_client[dbSynPosDining][collectionDiningList].find_one({'diningNo': diningNo})
        diningName = ''
        stallNos = []
        if res is None:
            return diningName, stallNos

        diningName = res.get('diningName')
        stallList = res.get('stallList')
        if stallList is None:
            return diningName, stallNos

        for v in stallList:
            stallInfo = {
                'stallNo': v.get('stallNo'),
                'stallName': v.get('stallName')
            }
            stallNos.append(stallInfo) 

        return diningName, stallNos
    except Exception as e:
        raise ErrMongoClient.data(str(e))


def nameSearch(diningName):
    '''
    餐厅模糊搜索
    '''
    try:
        mg_client = get_mongo_client()
        res = mg_client[dbSynPosDining][collectionDiningList].find()
        diningList = []
        for v in res:
            diningInfo = {
                'diningNo': v.get('diningNo'),
                'diningName': v.get('diningName'),
                'createTime': globalTimeToString(v.get('createTime')),
                'updateTime': globalTimeToString(v.get('updateTime')),
                'creatorUserName': v.get('creatorUserName'),
                'editorUserName': v.get('editorUserName'),
                'diningDesc': v.get('diningDesc'),
                'menuList': v.get('menuList')
            }
            diningList.append(diningInfo)

        res_diningList = []
        if len(diningName) > 0:
            for v in diningList:
                if diningName in v.get('diningName'):
                    res_diningList.append(v)
        else:
            res_diningList = diningList
        
        return res_diningList
    except Exception as e:
        raise ErrMongoClient.data(str(e))